package com.example.cdg;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import ualrcdg.files.GameEngine;
import ualrcdg.files.MediaResolver;
import ualrcdg.files.MyXMLHandler;
import ualrcdg.files.XMLHelper;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;



public class CDGMediaActivity extends Activity {
	public final static String EXTRA_MESSAGE = "com.example.myapp.MESSAGE";
	XMLHelper xmlhelper = null;
	 /** Called when the activity is first created. */
	 @Override	        
	        public void onCreate(Bundle savedInstanceState) {
	    		super.onCreate(savedInstanceState);
	    		//setContentView(R.layout.relative); 
	    		final String [] fileName;
	    		final String [] type;
	    		final String [] location;
	    		final String [] gameID;
	    		
	    		try {
	    			
	    			/** Handling XML */
	    			SAXParserFactory spf = SAXParserFactory.newInstance();
	    			SAXParser sp = spf.newSAXParser();
	    			XMLReader xr = sp.getXMLReader();

	    			/** Send URL to parse XML Tags */
	    			GameEngine ge = new GameEngine();
	    	    	ByteArrayInputStream bais = new ByteArrayInputStream(ge.getXML(this));
	    	    	InputSource is = new InputSource(bais);

	    			/** Create handler to handle XML Tags ( extends DefaultHandler ) */
	    			MyXMLHandler myXMLHandler = new MyXMLHandler();
	    			xr.setContentHandler(myXMLHandler);
	    			xr.parse(new InputSource(is.getByteStream()));
	    			
	    		} catch (Exception e) {
	    			System.out.println("XML Parsing Excepetion = " + e);
	    		}

	    		/** Get result from MyXMLHandler SitlesList Object */
	    		xmlhelper = MyXMLHandler.xmlhelper;

	    		/** Assign textview array length by arraylist size */
	    		fileName = new String[xmlhelper.getFileName().size()];
	    		type = new String[xmlhelper.getFileName().size()];
	    		location = new String[xmlhelper.getFileName().size()];
	    		gameID = new String[xmlhelper.getFileName().size()];
	    		

	    		/** Set the result text in textview and add it to layout */
	    		for (int i = 0; i < xmlhelper.getFileName().size(); i++) {
	    			fileName[i] = xmlhelper.getFileName().get(i);
	    			type[i] = xmlhelper.getType().get(i);
	    			location[i] = xmlhelper.getLocation().get(i);
	    			gameID[i] = xmlhelper.getGameID().get(i);
	    		}
	    		
	    		TextView textView = new TextView(this);
	    		textView.setText("Select your game ID number.");
	    		RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
	    		param.addRule(RelativeLayout.ALIGN_PARENT_TOP);
	    		textView.setId(555);
	    		textView.setLayoutParams(param);
	            Button button1 = new Button(this);  
	            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,  
	                    LayoutParams.WRAP_CONTENT); 
	            params.addRule(RelativeLayout.BELOW, 555);
	            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);  
	            // use same id as defined when adding the button  
	            params.addRule(RelativeLayout.LEFT_OF, 1001);  
	            button1.setLayoutParams(params);  
	            button1.setText("1");  
	            button1.setId(1111);
	            Button button2 = new Button(this); 
	            RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,  
	                    LayoutParams.WRAP_CONTENT);  
	            params2.addRule(RelativeLayout.BELOW, 555);
	            params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  
	            button2.setLayoutParams(params2);  
	            button2.setText("2");  
	            // give the button an id that we know  
	            button2.setId(1001);
	            Button button3 = new Button(this);  
	            RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,  
	                    LayoutParams.WRAP_CONTENT);  
	            params3.addRule(RelativeLayout.BELOW, 1001);
	            params3.addRule(RelativeLayout.BELOW, 1111);
	            button3.setLayoutParams(params3);  
	            button3.setText("Press here to parse XML"); 
	            RelativeLayout layout1 = new RelativeLayout(this);  
	            layout1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
	            layout1.addView(textView);
	            layout1.addView(button1);  
	            layout1.addView(button2);  
	            layout1.addView(button3);
	            setContentView(layout1);  
	            
	            button1.setOnClickListener(new View.OnClickListener() {
					
					public void onClick(View v) {
						// TODO Auto-generated method stub
						RelativeLayout layout2 = new RelativeLayout(CDGMediaActivity.this);
			            RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,  
			                    LayoutParams.WRAP_CONTENT);
			            params3.addRule(RelativeLayout.ALIGN_PARENT_TOP);
			            MediaResolver mr = new MediaResolver();
			            EditText ed1 = new EditText(CDGMediaActivity.this);
			            ed1.setLayoutParams(params3);
			            ed1.setHint(mr.getEdText(getApplicationContext(), 0));
			            ed1.setId(2002);
			            TextView textView = new TextView(CDGMediaActivity.this);
			            textView.setTextSize(20);
			            textView.setText(mr.getTextBox(getApplicationContext(), 0));
			            RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,  
			                    LayoutParams.WRAP_CONTENT);
			            params4.addRule(RelativeLayout.BELOW, 2002);
			            textView.setLayoutParams(params4);
			            layout2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));  
			            layout2.addView(ed1);  
			            layout2.addView(textView);
						setContentView(layout2);
					}});
	            
	            button2.setOnClickListener(new View.OnClickListener() {
					
					public void onClick(View v) {
						// TODO Auto-generated method stub
						RelativeLayout layout2 = new RelativeLayout(CDGMediaActivity.this);
			            RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,  
			                    LayoutParams.WRAP_CONTENT);
			            params3.addRule(RelativeLayout.ALIGN_PARENT_TOP);
			            MediaResolver mr = new MediaResolver();
			            EditText ed1 = new EditText(CDGMediaActivity.this);
			            ed1.setLayoutParams(params3);
			            ed1.setHint(mr.getEdText(getApplicationContext(), 1));
			            ed1.setId(2002);
			            TextView textView = new TextView(CDGMediaActivity.this);
			            textView.setTextSize(20);
			            textView.setText(mr.getTextBox(getApplicationContext(), 1));
			            RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,  
			                    LayoutParams.WRAP_CONTENT);
			            params4.addRule(RelativeLayout.BELOW, 2002);
			            textView.setLayoutParams(params4);
			            layout2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));  
			            layout2.addView(ed1);  
			            layout2.addView(textView);
						setContentView(layout2);
					}
				});
	            
	            button3.setOnClickListener(new View.OnClickListener() {
					
					public void onClick(View v) {
						// TODO Auto-generated method stub
						LinearLayout layout = new LinearLayout(CDGMediaActivity.this);
						layout.setOrientation(1);
						TextView fname = new TextView(CDGMediaActivity.this);
						fname.setText(fileName[0]);
						TextView myType = new TextView(CDGMediaActivity.this);
						myType.setText(type[0]);
						TextView locate = new TextView(CDGMediaActivity.this);
						locate.setText(location[0]);
						TextView gID = new TextView(CDGMediaActivity.this);
						gID.setText(gameID[0]); 
			            layout.addView(fname);  
			            layout.addView(myType);
			            layout.addView(locate);
			            layout.addView(gID);
						setContentView(layout);
					}});
	    } 
}